import { getConnect } from '@/api/common';
import { login, logout, getInfo } from '@/api/login';
import { getToken, setToken, removeToken, removeViceToken } from '@/utils/auth';
import store from '../index';

import Avatar1 from '@/images/0.gif';
import Avatar2 from '@/images/1.gif';
import Avatar3 from '@/images/2.jpg';
import Avatar4 from '@/images/3.gif';
import { getSaaSId, setSaaSId } from '../../utils/auth'

const random = Math.random() * 10;

const user = {
  state: {
    token: getToken(),
    id: '',
    SaaSId: '',
    name: '',
    sex: '',
    // avatar: 'https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif?imageView2/1/w/80/h/80',
    avatar: random < 3 ? Avatar1 : random < 6 ? Avatar2 : random < 8 ? Avatar3 : Avatar4,
    mobile: '',
    balance: '',
    position: '',
    last_login_time: '',
    company_name: '',
    email: '',
    openid: '',
    qy_wechat_id: '',
    created_at: '',
    work_status: 0,
    roles: [],
    pageSize: 50
  },

  mutations: {
    SET_TOKEN: (state, token) => {
      state.token = token;
    },
    SET_SAASID: (state, id) => {
      state.SaaSId = id;
      setSaaSId(id);
    },
    SET_NAME: (state, name) => {
      state.name = name;
    },
    SET_STATUS: (state, status) => {
      state.work_status = status;
    },
    SET_AVATAR: (state, avatar) => {
      state.avatar = avatar;
    },
    SET_INFO: (state, info) => {
      state.id = info.id;
      state.name = info.name;
      state.sex = info.sex;
      info.wechat_pic && (state.avatar = (info.wechat_pic.indexOf('http') >= 0 ? '' : store.getters.serverUrl) + info.wechat_pic);
      state.mobile = info.mobile;
      state.balance = info.bonus;
      state.position = info.position;
      state.last_login_time = info.last_login_time;
      state.email = info.email;
      state.created_at = info.created_at;
      state.openid = info.openid;
      state.qy_wechat_id = info.wechat_id; // 企业微信
      state.company_name = info.company_name;
      state.work_status = info.work_status;
      state.roleName = info.roleName;
      state.grade = info.grade;
      state.job_number = info.job_number;
      state.hiredate = info.hiredate;
      state.loginMode = info.loginMode;
    },
    SET_ROLES: (state, roles) => {
      state.roles = roles;
    }
  },

  actions: {
    SetToken({ commit }, token) {
      return new Promise((resolve, reject) => {
        commit('SET_TOKEN', token);
        resolve();
      })
    },

    // 登录
    Login({ commit }, userInfo) {
      const username = userInfo.username.trim()
      return new Promise((resolve, reject) => {
        login(username, userInfo.password, userInfo.imgKey, userInfo.imgCode).then(response => {
          const data = response.data;
          removeViceToken();
          setToken(data.token);
          commit('SET_TOKEN', data.token);
          commit('SET_SAASID', getSaaSId());
          resolve();
        }).catch(error => {
          reject(error);
        })
      })
    },

    // 扫码登录
    ScanCodeLogin({ commit }, code) {
      return new Promise((resolve, reject) => {
        const url = 'qyWexin';
        const data = { code: code };
        store.dispatch('GetConnect', { url, data }).then(res => {
          const data = res.data;
          removeViceToken();
          setToken(data.token);
          commit('SET_TOKEN', data.token);
          resolve();
        }).catch(error => {
          reject(error);
        })
      })
    },

    // 自动登录
    autoLogin({ commit }, data) {
      return new Promise((resolve, reject) => {
        const url = '/Nolandfall';
        store.dispatch('GetConnect', { url, data }).then(res => {
          const data = res.data;
          removeViceToken();
          setToken(data.token);
          commit('SET_TOKEN', data.token);
          commit('SET_SAASID', data.mallId);
          resolve();
        }).catch(error => {
          reject(error);
        })
      })
    },

    // 获取用户信息
    GetInfo({ commit }) {
      return new Promise((resolve, reject) => {
        getInfo().then(res => {
          if (res.code === 0) {
            commit('SET_INFO', res.data);
            commit('SET_SAASID', res.data.mallId)
            resolve(res)
          }
          reject()
        }).catch(error => {
          reject(error)
        })
      })
    },

    // 登出
    LogOut({ commit, state }) {
      return new Promise((resolve, reject) => {
        logout(state.token).then(() => {
          commit('SET_TOKEN', '')
          commit('SET_ROLES', [])
          removeViceToken()
          removeToken()
          // removeSaaSId();
          resolve()
        }).catch(error => {
          reject(error)
        })
      })
    },

    // 前端 登出
    FedLogOut({ commit }) {
      return new Promise(resolve => {
        commit('SET_TOKEN', '')
        removeViceToken()
        removeToken()
        // removeSaaSId()
        resolve()
      })
    },

    // 工作状态 接待|不接待
    SetStatus({ commit }, status) {
      return new Promise(resolve => {
        commit('SET_STATUS', status)
      })
    },

    // 用户头像
    SetAvatar({ commit }, avatar) {
      return new Promise(resolve => {
        commit('SET_AVATAR', avatar)
      })
    },

    // 通用链接接口方法
    GetConnect({ commit }, { url, data, type }) {
      return new Promise((resolve, reject) => {
        getConnect(url, data, type).then(response => {
          // const data = response.data
          resolve(response)
        }).catch(error => {
          reject(error)
        })
      })
    },

    // 上传文件接口方法
    UploadFile({ commit, state }, { url, data, type }) {
      return new Promise((resolve, reject) => {
        const formData = new FormData()
        for (const key in data) {
          formData.append(key, data[key])
        }
        formData.append('token', state.token)
        formData.append('mallId', state.SaaSId || getSaaSId())
        getConnect(url, formData, type).then(response => {
          // const data = response.data
          resolve(response)
        }).catch(error => {
          reject(error)
        })
      })
    },

    CreateAll({ commit }, { args }) {
      return Promise.all(args)
    }
  }
}

export default user
